package com.dongbao.service;

import com.alibaba.fastjson.JSON;
import com.dongbao.common.AuthorityEnum;
import com.dongbao.data.Datacenter;
import com.dongbao.data.MessageData;
import com.dongbao.dialog.CoreCodeAuditDialog;
import com.dongbao.dialog.MessageWindow;
import com.dongbao.entity.AuditInfo;
import com.dongbao.entity.MqMessage;
import com.dongbao.util.IdeaApiUtil;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;


import java.util.HashMap;
import java.util.Map;

import static com.dongbao.common.MessageConstant.*;

public class MqttRecieveCallback implements MqttCallback{

    private final NotificationGroup notificationGroup =  new NotificationGroup(AuthorityEnum.AUTHORITY_NOTIFICATION_TITLE, NotificationDisplayType.BALLOON,true);

    @Override
    public void connectionLost(Throwable cause) {

    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println("Client 接收消息主题 : " + topic);
        System.out.println("Client 接收消息Qos : " + message.getQos());
        System.out.println("Client 接收消息内容 : " + new String(message.getPayload()));

        /* 将消息转换为实体 */
        MqMessage mqMessage=JSON.parseObject(message.getPayload(), MqMessage.class);
        /* 处理消息内容 */
        if(mqMessage.getCode().equals(CORE_CLASS_AUDIT_MESSAGE)){
            /* 核心类代码提交审核消息 */
//            ExecutorService executor = Executors.newFixedThreadPool(1);
//            Future<Integer> future = executor.submit(new Callable<Integer>() {
//                @Override
//                public Integer call() throws Exception {
//                    Messages.showYesNoDialog("当前有人提交代码需要您进行审核操作！", "代码提交审核提醒", "审核通过", CommonBundle.getCancelButtonText(), UIUtil.getWarningIcon());
//                    Messages.showYesNoDialog(Datacenter.project , "当前有人提交代码需要您进行审核操作！", "代码提交审核提醒", "审核通过", CommonBundle.getCancelButtonText(), UIUtil.getWarningIcon());
//                    return 3;
//                }
//            });
            /* 打开代码审核的弹窗 */
            ApplicationManager.getApplication().invokeLater(() -> {
                CoreCodeAuditDialog dialog = new CoreCodeAuditDialog(Datacenter.project,mqMessage);
                dialog.open();
            });

            /* 代码传入窗口 */
        }else if(mqMessage.getCode().equals(ASSIGN_IP_MESSAGE)){
            /* 指定IP消息 */
            Notifications.Bus.notify(notificationGroup.createNotification(mqMessage.getMessage(), NotificationType.ERROR));
        }else if(mqMessage.getCode().equals(ALL_MESSAGE)){
            /* 全局消息提醒 */
            /* 将消息添加到数据交互中心的记录中 */
            MessageData messageData = JSON.parseObject(mqMessage.getData().toString(), MessageData.class);
            Datacenter.messageData.add(messageData);

            IdeaApiUtil.showNotification(messageData.getContent(),NotificationType.INFORMATION,Datacenter.project,"DHG内网消息[<span style=\"\">"+messageData.getSendIp()+"</span>]");

        }else if(mqMessage.getCode().equals(CORE_CLASS_AUDIT_PASS_MESSAGE)){
            /* 代码审核通过 */
            /* 消息提示审核通过 */
            IdeaApiUtil.showInfoNotification(mqMessage.getMessage(),Datacenter.project);
            /* 将通过的文件列表添加到忽略文件集合中 */
            for(Object item:mqMessage.getRows()){
                if(!Datacenter.auditIgnoreFiles.containsKey(item.toString())){
                    Map<String, String> filesContent=new HashMap<>();
                    filesContent.put(item.toString(),Datacenter.auditFilesContent.get(item.toString()));
                    Datacenter.auditIgnoreFiles.putAll(filesContent);
                }
            }
        }else if(mqMessage.getCode().equals(CORE_CLASS_AUDIT_NOT_PASS_MESSAGE)){
            /* 代码审核不通过 */
            IdeaApiUtil.showInfoNotification(mqMessage.getMessage(),Datacenter.project);

            AuditInfo auditInfo = JSON.parseObject(mqMessage.getData().toString(),AuditInfo.class);

        }

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {

    }

}